package top.lyc829.mymusic.ui.fragment.main

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.core.view.forEachIndexed
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import androidx.viewpager2.widget.ViewPager2
import com.tencent.mmkv.MMKV
import top.lyc829.mymusic.R
import top.lyc829.mymusic.adapter.ViewPager2Adapter
import top.lyc829.mymusic.base.BaseFragment
import top.lyc829.mymusic.databinding.FragmentMainBinding
import top.lyc829.mymusic.ui.fragment.index.IndexFragment
import top.lyc829.mymusic.ui.fragment.index.IndexViewModel
import top.lyc829.mymusic.ui.fragment.login.LoginFragment
import top.lyc829.mymusic.ui.fragment.profile.ProfileFragment
import top.lyc829.mymusic.util.Const
import top.lyc829.mymusic.viewmodel.AppViewModel

class MainFragment : BaseFragment<MainViewModel, FragmentMainBinding>() {

    companion object {
        fun newInstance() = MainFragment()
    }


    private var fragments : MutableList<Fragment> = ArrayList()
    val map = mutableMapOf<MenuItem, Int>()

    override fun getViewModel() =ViewModelProvider(this).get(MainViewModel::class.java)

    override fun initView(savedInstanceState: Bundle?) {
        binding.bottomNavigationView.menu.forEachIndexed { index, item ->
            map[item] = index
        }
        fragments.add(IndexFragment.newInstance())
        fragments.add(ProfileFragment.newInstance())

        binding.viewpager2.adapter = ViewPager2Adapter(context as FragmentActivity, fragments)
        //viewPager2.setUserInputEnabled(false);  // 禁止滑动
//        binding.viewpager2.offscreenPageLimit = fragments.size
    }

    override fun initClickListener() {
        binding.bottomNavigationView.setOnNavigationItemSelectedListener {
            binding.viewpager2.currentItem = map[it] ?: 0
            true
        }

        binding.viewpager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                binding.bottomNavigationView.menu.getItem(position).isChecked = true
            }
        })

        binding.bottomMusicView.setOnClickListener {
            findNavController().navigate(R.id.action_mainFragment_to_musicPlayerFragment)
        }

    }

    override fun createObserver() {
        appViewModel.isLogin.observe(viewLifecycleOwner) {
            if (!it) {
                findNavController().navigate(R.id.action_global_loginFragment)
            }
        }
    }

    override fun initData() {
        appViewModel.isLogin.value = MMKV.defaultMMKV().decodeString(Const.TOKEN) != null
    }

    override fun getBinding(inflater: LayoutInflater) = FragmentMainBinding.inflate(inflater)

}